package com.pipe.protocol.events.common;

import com.pipe.util.DateUtils;
import io.netty.buffer.ByteBuf;

/**
 * @author Kiven
 */
public class QueryEventPostHeader {
    private long slaveProxyId;
    private long executionTime;
    private byte schemaLength;
    private int errorCode;
    private int statusVarsLength;

    public QueryEventPostHeader(long slaveProxyId, long executionTime, byte schemaLength, int errorCode, int statusVarsLength) {
        this.slaveProxyId = slaveProxyId;
        this.executionTime = executionTime;
        this.schemaLength = schemaLength;
        this.errorCode = errorCode;
        this.statusVarsLength = statusVarsLength;
    }

    public static QueryEventPostHeader build(ByteBuf byteBuf){
        long spi = byteBuf.readUnsignedIntLE();
        long et = (long)byteBuf.readUnsignedIntLE() * 1000;
        byte sl = byteBuf.readByte();
        int ec = byteBuf.readUnsignedShortLE();
        int sv = byteBuf.readUnsignedShortLE();
        return new QueryEventPostHeader(spi, et, sl, ec, sv);
    }

    public byte getSchemaLength() {
        return schemaLength;
    }

    public int getStatusVarsLength() {
        return statusVarsLength;
    }

    @Override
    public String toString() {
        return "QueryEventPostHeader{" +
                "slaveProxyId=" + slaveProxyId +
                ", executionTime=" + DateUtils.timeStampToDate(executionTime) +
                ", schemaLength=" + schemaLength +
                ", errorCode=" + errorCode +
                ", statusVarsLength=" + statusVarsLength +
                '}';
    }
}